////////////////////////////////////////////////////////////////////////////////
/*! \file
 * \brief The implementation file for the MythDynamic library.
 *
 * \author Ivo Wingelaar
 * \date 2011
 * \version 0.1.1
 * \copyright GNU Lesser General Public License v3
 */
////////////////////////////////////////////////////////////////////////////////

#include "MythDynamic.hpp"
#include "MythDynamic_LibraryLinux_C.hpp"
#include "MythDynamic_LibraryWindows_C.hpp"

namespace Myth
{
namespace Dynamic
{

////////////////////////////////////////////////////////////////////////////////
//! Try to load the library in memory and return a handle to it on success.
_MYTH_DYNAMIC_API_ Library* _MYTH_CALL_CONV_ create_dynamic_library(const std::string& file_name, std::string* error_message)
{
#ifdef _MYTH_LINUX_
    LibraryLinux_C *lib = new LibraryLinux_C(file_name);
#endif // _MYTH_LINUX_
#ifdef _MYTH_WINDOWS_
    LibraryWindows_C *lib = new LibraryWindows_C(file_name);
#endif // _MYTH_WINDOWS_

    if(lib->is_valid())
        return lib;
    else
    {
        if(error_message)
            *error_message = lib->get_error();

        lib->decrement_counter();
        return 0;
    }
}

////////////////////////////////////////////////////////////////////////////////
//! Returns a handle to the currently running executable.
_MYTH_DYNAMIC_API_ Library* _MYTH_CALL_CONV_ create_dynamic_self_reference(void)
{
#ifdef _MYTH_LINUX_
    return new LibraryLinux_C();
#endif // _MYTH_LINUX_
#ifdef _MYTH_WINDOWS_
    return new LibraryWindows_C();
#endif // _MYTH_WINDOWS_
}

////////////////////////////////////////////////////////////////////////////////
//! Returns the version of the MythDynamic library.
_MYTH_DYNAMIC_API_ uint32_t _MYTH_CALL_CONV_ get_myth_dynamic_version(void)
{
    return MYTH_SDK_VERSION;
}

} // namespace Dynamic
} // namespace Myth
